package com.mengjiahui.auduoplayer.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageButton;

import com.mengjiahui.auduoplayer.fragment.MainFragment;
import com.mengjiahui.auduoplayer.MusicFragmentPagerAdapter;
import com.mengjiahui.auduoplayer.service.NativeMusicService;
import com.mengjiahui.auduoplayer.fragment.OnlineMusicFragment;
import com.mengjiahui.auduoplayer.R;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private Button localButton;
    private Button onlineButton;
    private ImageButton playButton;
    private ImageButton imageButtonPlayList;
    private ViewPager viewPager;
    private List<Fragment> list;
    private MusicFragmentPagerAdapter musicFragmentPagerAdapter;
    private NativeMusicService.MyBinder myBinder;
//    private ServiceConnection connection = new ServiceConnection() {
//        @Override
//        public void onServiceConnected(ComponentName name, IBinder service) {
//            myBinder = (NativeMusicService.MyBinder) service;
//        }
//
//        @Override
//        public void onServiceDisconnected(ComponentName name) {
//
//        }
//    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = findViewById(R.id.toolbar);
        list = new ArrayList<>();
        list.add(new MainFragment());
        list.add(new OnlineMusicFragment());
        musicFragmentPagerAdapter = new MusicFragmentPagerAdapter(getSupportFragmentManager(), list);
        viewPager = findViewById(R.id.viewpager);
        viewPager.setAdapter(musicFragmentPagerAdapter);
        viewPager.setCurrentItem(0);
        viewPager.addOnPageChangeListener(new MyPagerChangeListener());
        imageButtonPlayList = findViewById(R.id.buttonPlayList);
        imageButtonPlayList.setOnClickListener(view -> {
            Intent intent = new Intent(MainActivity.this, PlayListActivity.class);
            startActivity(intent);
        });
        playButton = findViewById(R.id.buttonPlay);
        playButton.setOnClickListener(view -> {
            MainFragment item = (MainFragment) musicFragmentPagerAdapter.getItem(0);
            myBinder=item.myBinder;
            myBinder.playOrPause();
        });
    }
}

class MyPagerChangeListener implements ViewPager.OnPageChangeListener {

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        System.out.println("state:  " + state);
    }
}
